package org.linlinjava.litemall.admin.service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import org.linlinjava.litemall.admin.dto.FormulaDTO;
import org.linlinjava.litemall.admin.dto.MaterialAttribute;
import org.linlinjava.litemall.admin.dto.MaterialVO;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.Formula;
import org.linlinjava.litemall.db.domain.Material;
import org.linlinjava.litemall.db.domain.Product;
import org.linlinjava.litemall.db.service.MaterialService;
import org.linlinjava.litemall.db.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import static org.linlinjava.litemall.admin.util.AdminResponseCode.GOODS_NAME_EXIST;
import static org.linlinjava.litemall.admin.util.AdminResponseCode.MATERIAL_NOT_EXIST;

/**
 * Author tony
 * Description
 * Date 2020/10/23 0:21
 */
@Service
public class AdminProductService {


    @Resource
    private ProductService productService;

    @Resource
    private MaterialService materialService;

    @Transactional
    public Object addOne(FormulaDTO brand) {
        if (CollectionUtils.isEmpty(brand.getAttributes())) {
            return ResponseUtil.fail(MATERIAL_NOT_EXIST, "原料不能为空");
        }

        Formula formula = brand.getFormula();
        List<MaterialAttribute> attributes = brand.getAttributes();

        String name = formula.getName();
        Product product = new Product();
        BeanUtils.copyProperties(formula,product);

        if (productService.checkExistByName(name)) {
            return ResponseUtil.fail(GOODS_NAME_EXIST, "产品名已经存在");
        }
        productService.add(product);
        Integer productId = product.getId();
        List<Integer> materialIds = Lists.newArrayList();
        Map<Integer, BigDecimal> maps = Maps.newHashMap();
        for (MaterialAttribute material : attributes) {
            materialIds.add(material.getMaterialId());
            maps.put(material.getMaterialId(), material.getAmount());
        }
        List<Material> materials = materialService.queryByIds(materialIds);
        BigDecimal totalPrice = BigDecimal.ZERO;
        List<MaterialVO> materialAttributes = Lists.newArrayList();
        for (Material m : materials) {
            BigDecimal price = m.getPrice();
            BigDecimal amount = maps.get(m.getId());
            totalPrice = totalPrice.add(price.multiply(amount));
            MaterialVO materialAttribute = new MaterialVO();
            materialAttribute.setId(m.getId());
            materialAttribute.setName(m.getName());
            materialAttribute.setUnit(m.getUnit());
            materialAttribute.setAmount(amount);
            materialAttribute.setPrice(price);
            materialAttribute.setTotalPrice(price.multiply(amount));
            materialAttributes.add(materialAttribute);
        }
        Product p = new Product();
        p.setPrice(totalPrice);
        p.setId(productId);
        p.setMaterial(new Gson().toJson(materialAttributes));
        productService.updateById(p);
        return ResponseUtil.ok();
    }
}
